

import 'dart:async';

import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:oral_cloud/config/app_config.dart';
import 'package:oral_cloud/config/constant.dart';
import 'package:oral_cloud/config/resource_mananger.dart';
import 'package:oral_cloud/config/router_manger.dart';
import 'package:shared_preferences/shared_preferences.dart';
import 'package:oral_cloud/config/storage_manager.dart';

class SplashPage extends StatefulWidget{

  @override
  State<StatefulWidget> createState() {
    // TODO: implement createState
    return _SplashPageState();
  }
  
}

class _SplashPageState extends State<SplashPage>{

//  Timer countdownTimer;

  int seconds=1000;
  @override
  void initState() {
//    countdownTimer=Timer.periodic(new Duration(milliseconds: 500), (timer) {
//        if(seconds>0){
//          seconds-=500;
//        }else{
//          countdownTimer.cancel();
//          countdownTimer = null;
//          _checkLogin();
//        }
//    });
    _checkLogin();
    super.initState();
  }


  void _checkLogin() async {
    final prefs=await SharedPreferences.getInstance();
    int loginState=prefs.getInt(Constant.kLoginStatue);
    print("loginState=$loginState");
    if(loginState==LoginState.logined){
      _jump2HomePage();
    }else{
      _jump2LoginPage();
    }
  }


  void _jump2HomePage(){
    print('跳转到主页');
    Navigator.of(context).pushReplacementNamed(RouteName.home);
  }
  void _jump2LoginPage(){
    print('跳转到登录');
    Navigator.pushReplacementNamed(context, RouteName.login);
//    Navigator.of(context).pushNamed(RouteName.login);

  }

  @override
  Widget build(BuildContext context) {


    return Scaffold(
      body: OrientationBuilder(
        builder: (context,orentation){
          AppConfig.isPad=orentation!=Orientation.portrait;
          print('isPad=${AppConfig.isPad}');
          ScreenUtil.instance = ScreenUtil(width: AppConfig.isPad?2048:1080, height: AppConfig.isPad?1536:1920)..init(context);
          return WillPopScope(
            onWillPop: ()=>Future.value(false),
            child: Image.asset(ImageHelper.wrapAssets('splash_bg.png'),width: double.infinity,height: double.infinity,),
          );
        },
      ),
    );
  }
  
}